package LeetCode.interview;

import com.sun.jndi.url.iiopname.iiopnameURLContextFactory;
import com.sun.swing.internal.plaf.basic.resources.basic;

import util.LogUtils;

/*
 * 
原题

　　Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 

题目大意

　　给定一个m*n的矩阵，如果某个位置是0。将对应的行和列设置为0。 

解题思路
	建立 一模一样的数组
   @Difficult 	medium
 * @Date 		2017-09-13 14：25
 */
public class _073_Set_Matrix_Zeroes {
    public _073_Set_Matrix_Zeroes setZeroes(int[][] matrix) {
    	boolean[] flagRow = new boolean[matrix.length];
    	boolean[] flagCol = new boolean[matrix[0].length];
    	
    	for (int i = 0; i < matrix.length; i ++) {
    		for (int j = 0; j < matrix[0].length; j ++) {
    			if (matrix[i][j] == 0) {
    				flagRow[i] = true;
    				flagCol[j] = true;
    			}
    		}
    	}
    	
    	for (int i = 0; i < matrix.length; i ++) {
    		for (int j = 0; j < matrix[0].length; j ++) {
    			if (flagRow[i]) {
    				matrix[i][j] = 0;
    			}
    			if (flagCol[j]) {
    				matrix[i][j] = 0;
    			}
    		}
    	}
    	//遍历：看下结果
    	traverse(matrix);
        return this;
    }
    
    public void  traverse(int[][] matrix) {
    	for (int i = 0; i < matrix.length; i ++) {
    		for (int j = 0; j < matrix[0].length; j ++) {
    			LogUtils.print(matrix[i][j]);
    		}
    		LogUtils.br();
    	}
    }
	

	public static void main(String[] args) {
		LogUtils.println("结果", new _073_Set_Matrix_Zeroes().setZeroes(
				new int[][] {
					{1, 1, 1, 1, 1},
					{1, 0, 1, 1, 1},
					{1, 1, 1, 1, 1},
					{1, 1, 0, 1, 1},
					{1, 1, 1, 1, 1},
					{1, 0, 1, 1, 1},
					{1, 1, 1, 1, 1},
				}
				));
	}
}
